package pers.weiney.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.weiney.mapper.CategoryMapper;
import pers.weiney.pojo.Category;
import pers.weiney.service.CategoryService;
import pers.weiney.utils.ThreadLocalUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Author: @Weiney
 * @ClassName: CategoryServiceImpl
 * @Description: 文章分类服务层接口实现类
 * @Create: 2024-08-13
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        // 补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        category.setCreateUser(id);
        categoryMapper.insertCategory(category);
    }

    @Override
    public List<Category> list() {
        /*Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");*/
        Integer id = ThreadLocalUtil.getId();
        return categoryMapper.selectAll(id);
    }

    @Override
    public Category getCategory(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.delete(id);
    }
}